package me.project.android_service.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.project.android_service.mapper.AndroidInfoMapper;
import me.project.android_service.service.IAndroidInfoService;
import me.project.android_common.domain.AndroidInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class AndroidInfoServiceImpl extends ServiceImpl<AndroidInfoMapper, AndroidInfo> implements IAndroidInfoService {
    @Resource
    private AndroidInfoMapper androidInfoMapper;

//    @Autowired
//    private DataPointMapper dataPointMapper;
//
//    @Autowired
//    private DataTypeMapper dataTypeMapper;
    @Override
    public boolean existsWithAppNameOrPackageName(String appName, String packageName) {
        QueryWrapper<AndroidInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("appName", appName).or().eq("packageName", packageName);

        // 执行查询并根据结果返回 true 或 false
        int count = androidInfoMapper.selectCount(queryWrapper);
        return count > 0;  // 当找到一个或多个记录时返回 true
    }

//    @Override
//    public AndroidInfoDto selectAndroidInfoWithPoints(String uuid) {
//        // 步骤1: 查询AndroidInfo
//        AndroidInfo androidInfo = super.selectOne(new EntityWrapper<AndroidInfo>().eq("uuid", uuid));
//
//        // 步骤2: 查询相关的DataPoint
//        List<DataPoint> dataPoints = dataPointMapper.selectList(new EntityWrapper<DataPoint>().eq("androidId",androidInfo.getId()));
//
//        // 步骤3: 按DataType分类DataPoint
//        Map<Long, Set<DataPoint>> classifiedDataPoints = new HashMap<>();
//        for (DataPoint dataPoint : dataPoints) {
//            DataType dataType = dataTypeMapper.selectById(dataPoint.getDataTypeId());
//            classifiedDataPoints.computeIfAbsent(dataType.getId(), k -> new HashSet<>()).add(dataPoint);
//        }
//
//        // 步骤4: 组装返回结果
//        return new AndroidInfoDto(androidInfo, classifiedDataPoints);
//    }


    @Override
    public AndroidInfo selectAndroidInfoWithPoints(String uuid) {
        return androidInfoMapper.selectAndroidInfoWithPoints(uuid);
    }

    @Override
    public boolean existsWithAppName(String appName) {
        return androidInfoMapper.selectCount(new QueryWrapper<AndroidInfo>().eq("appName",appName)) == 0;
    }

    @Override
    public boolean existsWithPackageName(String packageName) {
        return androidInfoMapper.selectCount(new QueryWrapper<AndroidInfo>().eq("packageName",packageName)) == 0;
    }
}
